const app = getApp();
const utils = require('../../utils/util')
Page({
  data: {
    sections: ['公司介绍', '公司文化', '发展历程', '公司荣誉', '主营业务'],
    currentIndex: 0,
    currentNavItem: '',
    sectionHeights: [],
    isScrolling: false,
    active: 0,
    swiper_list: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    company_info: {
      about_bg_cover: '',
      introduce: '',
      company_culture: [],
      history_introduce: '',
      company_history: [],
      honor_introduce: '',
      company_honor: [],
      business_introduce: '',
      company_business: [],
      qrcode1: '',
      qrcode2: '',
      address: '',
      website: '',
      tel: '',
    },
    scrollTop: 0, // 用于存储滑动距离
    scrollTopNew: 0,  //滚动距离
    stickyTopHeight: 0, //tabs距离顶部的高度
    navT: 0,  //自定义导航栏距离顶部margin-top
    navH: 0,   //自定义导航栏行高
    oneFixed: '', //吸顶样式
    scrollLeft: 0,
    list: [{   //tabs
      id: 0,
      title: '公司介绍'
    },
    {
      id: 1,
      title: '公司文化'
    },
    {
      id: 2,
      title: '发展历程'
    },
    {
      id: 3,
      title: '公司荣誉'
    },
    {
      id: 4,
      title: '主营业务'
    },
    ],
    currentIndex: 0, //当前选中tabs
  },

  onLoad: function () {
    let that = this
    that.init_data();
    that.getNav()
  },
  // 加载公司信息介绍
  init_data() {
    var url = 'index/compnay_detail'
    getApp().util.request({
      url: url,
    }).then((res) => {
      this.setData({
        company_info: res,
      })
    })
  },


  left_click() {
    let active = this.data.active - 1;
    if (active < 0) {
      active = 0;
    }
    console.log(active);
    this.setData({
      active: active
    });
  },
  right_click() {
    let active = this.data.active + 1;
    if (active > 2) {
      active = 2;
    }
    console.log(active);
    this.setData({
      active: active
    });
  },
  // calculateSectionHeights: function () {
  //   const query = wx.createSelectorQuery().in(this);
  //   const sectionHeights = [];
  //   this.data.sections.forEach((item, index) => {
  //     query.select(`#section${index + 1}`).boundingClientRect(rect => {
  //       if (rect) {
  //         sectionHeights.push(rect.height);
  //         if (sectionHeights.length === this.data.sections.length) {
  //           this.setData({ sectionHeights });
  //           this.updateActiveNavItem();
  //         }
  //       }
  //     }).exec();
  //   });
  // },

  tabChange(e) {
    let that = this;
    let selector = '#cont' + e.currentTarget.dataset.index
    let offsetTop = -50 + that.data.navT + that.data.navH
    wx.pageScrollTo({
      offsetTop: -offsetTop,
      selector: selector,
      duration: 0,
      success: function (e) {
        console.log(that.data.currentIndex);
        console.log("成功", e)
        if(that.data.currentIndex == 3 || that.data.currentIndex == 4){
          that.setData({
            scrollLeft: 5000
          });
        }else{
          that.setData({
            scrollLeft: 0
          });
        }
      },
      file: function (e) {
        console.log("失败", e)
      }
    })
    this.setData({
      currentIndex: e.currentTarget.dataset.index,
    })
  },

  getNav: function () {
    let that = this
    const res = wx.getMenuButtonBoundingClientRect()
    that.setData({
      navT: res.top,
      navH: res.height
    })
  },
  onPageScroll: utils.throttle(function (res) {
    let event = res[0];
    let that = this;
    let scrollTopNew = event.scrollTop;
    let height = this.data.navT + this.data.navH;
    const query = wx.createSelectorQuery();
    let itemCount = that.data.list.length;
    for (let item of that.data.list) {
      query.select('#cont' + item.id).boundingClientRect();
    }
    query.exec(function (rect) {
      let currentIndex = 0;
      if (rect[itemCount - 1].top <= (height + 50)) {
        console.log('最后一个');
        that.setData({
          scrollLeft: 1000
        });
      } else {
        that.setData({
          scrollLeft: 0
        });
      }
      for (let i in rect) {
        if (rect[i].top <= (height + 50)) {
          currentIndex = i;
        } else {
          break;
        }
      }
      that.setData({
        currentIndex: currentIndex
      });
    });
  }, 100),
  // updateActiveNavItem: function () {
  //     console.log(this.data.currentIndex);
  //     this.setData({
  //         currentIndex: this.data.currentIndex,
  //         scrollLeft: (this.data.currentIndex - 2) * 200
  //     });
  //     const scrollTop = this.data.scrollTop;
  //     const sectionHeights = this.data.sectionHeights;
  //     let sumHeight = 0;
  //     let currentIndex = 0;
  //     for (let i = 0; i < sectionHeights.length;i++) {
  //         sumHeight += sectionHeights[i];
  //         if (scrollTop < sumHeight) {
  //             currentIndex = i;
  //             break;
  //         }
  //     }
  //     const id = `section${currentIndex + 1}`;
  //     this.setData({
  //         currentIndex: currentIndex,
  //         currentNavItem: id
  //     });
  // },
  // onPageScroll: function (e) {
  //     if (this.data.isScrolling) return;

  //     this.setData({
  //         scrollTop: e.scrollTop
  //     });

  //     this.updateActiveNavItem();
  // },
  // scrollToSection: function (e) {
  //     console.log(e.currentTarget.dataset.index);
  //     this.setData({
  //         currentIndex: e.currentTarget.dataset.index,
  //         scrollLeft: (e.currentTarget.dataset.index - 2) * 200
  //     })
  //     if (this.data.isScrolling) return;
  //     const index = e.currentTarget.dataset.index;
  //     const id = `section${index + 1}`;
  //     console.log(id);
  //     this.setData({
  //         currentIndex: index,
  //         currentNavItem: id,
  //         isScrolling: true
  //     });

  //     wx.pageScrollTo({
  //         selector: `#${id}`,
  //         duration: 300,
  //         success: () => {
  //             this.setData({
  //                 isScrolling: false
  //             });
  //         },
  //         fail: () => {
  //             this.setData({
  //                 isScrolling: false
  //             });
  //         }
  //     });
  // }
})